<script setup>
  import { throttle } from 'licia'
  import { patientConsultOrderListAPI } from '@/services/consult.js'
  import { nextTick, ref } from 'vue'
  const props = defineProps({
    type: Number,
    default: '1'
  })
  // 分页页码
  const current = ref(1)
  // 每页数据条数
  const pageSize = 5
  // 加载状态提示
  const loadingStatus = ref('more')
  const refresherTriggered = ref(false)
  const orderList = ref([])
  const getOrderList = async (loading) => {
    if (loadingStatus.value === 'noMore') return
    loadingStatus.value = 'loading'
    const res = await patientConsultOrderListAPI(
      {
        current: current.value,
        pageSize,
        type: props.type
      },
      loading
    )
    for (let v of res.data.data.rows) {
      v.isShow = false
    }
    orderList.value.push(...res.data.data.rows)
    loadingStatus.value = 'more'
    current.value++
    if (current.value > res.data.data.pageTotal) loadingStatus.value = 'noMore'
  }
  //下拉触底事件
  const onScrolltolower = throttle(() => {
    getOrderList()
  }, 300)
  //上拉刷新
  const onRefresherrefresh = async () => {
    refresherTriggered.value = true
    await nextTick()
    refresherTriggered.value = false
    current.value = 1
    loadingStatus.value = 'more'
    orderList.value = []
    await getOrderList(false)
  }
  getOrderList()
</script>

<template>
  <scroll-view
    refresher-enabled
    refresher-background="#f6f6f6"
    :refresher-triggered="refresherTriggered"
    class="uni-scroll-view"
    scroll-y
    @scrolltolower="onScrolltolower"
    @refresherrefresh="onRefresherrefresh"
  >
    <view class="consult-list">
      <view
        v-for="order in orderList"
        :key="order.id"
        class="consult-list-item"
      >
        <view class="consult-header">
          <view class="label">
            <image class="doctor-avatar" :src="order.docInfo.avatar" />
            <text>{{ order.typeValue }}（自动分配医生）</text>
          </view>
          <text v-if="order.status === 1" class="status color-1">待支付</text>
          <text v-if="order.status === 2" class="status color-2">待接诊</text>
          <text v-if="order.status === 3" class="status color-2">咨询中</text>
          <text v-if="order.status === 4" class="status color-3">已完成</text>
          <text v-if="order.status === 5" class="status color-3">已取消</text>
        </view>
        <navigator
          class="consult-body"
          hover-class="none"
          :url="`/subpkg_consult/order_detail/index?id=${order.id}`"
        >
          <uni-list :border="false">
            <uni-list-item
              :border="false"
              title="病情描述"
              :right-text="order.illnessDesc"
            />
            <uni-list-item
              :border="false"
              title="价格"
              :right-text="'¥' + order.payment"
            />
            <uni-list-item
              :border="false"
              title="创建时间"
              :right-text="order.createTime"
            />
          </uni-list>
        </navigator>

        <view v-if="order.status === 1" class="consult-footer">
          <button class="uni-button minor">取消订单</button>
          <button class="uni-button">去支付</button>
        </view>
        <view v-if="order.status === 2" class="consult-footer">
          <button class="uni-button minor">取消订单</button>
          <button class="uni-button">继续咨询</button>
        </view>
        <view v-if="order.status === 3" class="consult-footer">
          <button class="uni-button">继续咨询</button>
        </view>
        <view v-if="order.status === 4" class="consult-footer">
          <button class="uni-button minor">问诊记录</button>
          <button v-if="order.evaluateId" class="uni-button minor">
            查看评价
          </button>
          <button v-else class="uni-button">去评价</button>
        </view>
        <view v-if="order.status === 5" class="consult-footer">
          <view class="uni-button more" @click="order.isShow = !order.isShow">
            <text class="label">更多</text>
            <uni-transition :show="order.isShow">
              <view class="list">
                <view class="list-item">查看处方</view>
                <view class="list-item">删除订单</view>
              </view>
            </uni-transition>
          </view>
          <button class="uni-button minor">删除订单</button>
          <button class="uni-button">咨询其它医生</button>
        </view>
      </view>

      <!-- 加载状态 -->
      <uni-load-more
        :status="loadingStatus"
        color="#C3C3C5"
        :icon-size="16"
        :content-text="{
          contentdown: '上拉显示更多',
          contentrefresh: '数据正在加载中...',
          contentnomore: '没有更多数据了'
        }"
      />
    </view>
  </scroll-view>
</template>

<style lang="scss">
  .uni-scroll-view {
    height: calc(100vh - 50px);
    /* #ifdef H5 */
    height: calc(100vh - 94px);
    /* #endif */
    overflow: hidden;
    box-sizing: border-box;
    padding-top: 15rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  }

  .consult-list {
    padding: 15rpx 30rpx 30rpx;
  }

  .consult-list-item {
    // height: 715rpx;
    padding: 0 30rpx;
    margin-bottom: 30rpx;
    border-radius: 10rpx;
    background-color: #fff;

    .consult-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100rpx;
      padding: 0 30rpx;
      margin: 0 -30rpx;
      font-size: 30rpx;
      border-bottom: 1rpx solid #f5f5f5;

      .label {
        display: flex;
        align-items: center;
        color: #000;
        font-weight: 500;
      }

      .doctor-avatar {
        width: 40rpx;
        height: 40rpx;
        margin-right: 10rpx;
        border-radius: 50%;
      }

      .status {
        color: #121826;
      }

      .color-1 {
        color: #f2994a;
      }

      .color-2 {
        color: #16c2a3;
      }

      .color-3 {
        color: #848484;
      }
    }

    .consult-body {
      padding: 20rpx 0;
    }

    :deep(.uni-list-item__container) {
      padding: 10rpx 0 !important;
    }

    :deep(.uni-list-item__content-title) {
      font-size: 28rpx !important;
      color: #c3c3c5 !important;
    }

    :deep(.uni-list-item__extra-text) {
      font-size: 28rpx !important;
      color: #333 !important;
    }

    :deep(.uni-list-item__extra) {
      width: 480rpx !important;
      justify-content: flex-start !important;
    }

    .consult-footer {
      display: flex;
      justify-content: flex-end;
      padding: 30rpx 0;
      border-top: 1rpx solid #f5f5f5;

      .uni-button {
        height: 60rpx;
        line-height: 60rpx;
        /* #ifdef MP */
        line-height: 59rpx;
        /* #endif */
        margin: 0 0 0 30rpx;
        font-size: 26rpx;
        color: #16c2a3;
        border: 1rpx solid #16c2a3;
        background-color: #fff;

        &.more {
          flex: 1;
          text-align: left;
          margin-left: 0;
          border-color: transparent;
          color: #979797;
          position: relative;
        }

        .list {
          position: absolute;
          left: 0;
          bottom: 75rpx;
          z-index: 99;
          width: 160rpx;
          text-align: center;
          padding: 10rpx 30rpx;
          border-radius: 10rpx;
          font-size: 28rpx;
          box-shadow: 0 0 30rpx rgba(50, 50, 51, 0.3);
          background-color: #fff;

          &::after {
            position: absolute;
            bottom: -28rpx;
            left: 30rpx;
            content: '';
            border-width: 14rpx;
            border-style: solid;
            border-color: #fff transparent transparent transparent;
          }
        }

        .list-item {
          padding: 10rpx 0;
          border-bottom: 1rpx solid #eee;

          &:last-child {
            border-bottom: none;
          }
        }

        &.minor {
          color: #3c3e42;
          border-color: #ededed;
          background-color: #fafafa;
        }
      }
    }
  }
</style>
